<template>
  <div class="checkbox-wrap" @click="changeChecked()">
    <i v-if="checked" class="iconfont icon-duoxuan"></i>
    <i v-else class="iconfont icon-duoxuan_weixuanzhong"></i>
    <span v-if="$slots.default"><slot /></span>
  </div>
</template>

<script setup>
import { ref,watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['change'])
const checked = ref(false)
const changeChecked = () => {
    checked.value = !checked.value
    emit('change', checked.value)
}

// 使用侦听器，得到父组件传递数据，给checked数据
watch(() => props.modelValue, () => {
  checked.value = props.modelValue
}, { immediate: true })
</script>

<style scoped lang="scss">
.checkbox-wrap {
  display: inline-block;
  margin-right: 2px;
  .icon-checked {
    color: $main-color;
    ~ span {
      color: $main-color;
    }
  }
  i {
    position: relative;
    top: 1px;
  }
  span {
    margin-left: 2px;
  }
}
</style>